﻿Public Class Shell

    Const SHELLITEM_GUID As String = "43826d1e-e718-42ee-bc55-a1e261c37bfe"

    <Flags()>
    Private Enum SIIGBF
        SIIGBF_RESIZETOFIT = 0
        SIIGBF_BIGGERSIZEOK = 1
        SIIGBF_MEMORYONLY = 2
        SIIGBF_ICONONLY = 4
        SIIGBF_THUMBNAILONLY = 8
        SIIGBF_INCACHEONLY = 16
    End Enum

    Private Enum SIGDN As UInteger
        NORMALDISPLAY = 0
        PARENTRELATIVEPARSING = &H80018001UI
        PARENTRELATIVEFORADDRESSBAR = &H8001C001UI
        DESKTOPABSOLUTEPARSING = &H80028000UI
        PARENTRELATIVEEDITING = &H80031001UI
        DESKTOPABSOLUTEEDITING = &H8004C000UI
        FILESYSPATH = &H80058000UI
        URL = &H80068000UI
    End Enum

    Public Shared Function GetFileThumbnail(path As String) As Bitmap
        Dim ppsi As IShellItem = Nothing
        Dim hbitmap As IntPtr = IntPtr.Zero
        Dim uuid As Guid

        Try
            uuid = New Guid(SHELLITEM_GUID)
            SHCreateItemFromParsingName(path, IntPtr.Zero, uuid, ppsi)
            CType(ppsi, Shell.IShellItemImageFactory).GetImage(New Size(150, 150), 0, hbitmap)

            Return Bitmap.FromHbitmap(hbitmap)

        Catch ex As Exception
            Return Nothing

        End Try

    End Function

    <DllImport("shell32.dll", CharSet:=CharSet.Unicode, PreserveSig:=False)>
    Private Shared Sub SHCreateItemFromParsingName(
          <MarshalAs(UnmanagedType.LPWStr)>
          ByVal pszPath As String,
          ByVal pbc As IntPtr,
          <MarshalAs(UnmanagedType.LPStruct)>
          ByVal riid As Guid,
          <MarshalAs(UnmanagedType.Interface, IidParameterIndex:=2)>
          ByRef ppv As IShellItem)
    End Sub

    <ComImportAttribute(), GuidAttribute("bcc18b79-ba16-442f-80c4-8a59c30c463b"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)>
    Private Interface IShellItemImageFactory
        Sub GetImage(ByVal size As Size, ByVal flags As SIIGBF, ByRef phbm As IntPtr)
    End Interface

    <ComImport(), Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Private Interface IShellItem
        Sub BindToHandler(ByVal pbc As IntPtr,
        <MarshalAs(UnmanagedType.LPStruct)>
        ByVal bhid As Guid,
        <MarshalAs(UnmanagedType.LPStruct)>
        ByVal riid As Guid,
         ByRef ppv As IntPtr)

        Sub GetParent(ByRef ppsi As IShellItem)

        Sub GetDisplayName(ByVal sigdnName As SIGDN, ByRef ppszName As IntPtr)

        Sub GetAttributes(ByVal sfgaoMask As UInt32, ByRef psfgaoAttribs As UInt32)

        Sub Compare(ByVal psi As IShellItem, ByVal hint As UInt32, ByRef piOrder As Integer)

    End Interface

End Class
